package org.lex.keytool.ui.task;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;

import org.lex.keytool.Swings;
import org.lex.keytool.core.service.KeyTools;
import org.lex.keytool.core.task.ImportCertificateTask;

public class ImportCertificateTaskPane extends BaseTaskPane {
	public static void main(String[] args) {
		Swings.show(new ImportCertificateTaskPane());
	}

	private JButton sourceButton;
	private JButton destButton;
	private JCheckBox trustCacertsCheckBox;
	private JComboBox storeTypeComboBox;
	private JLabel sourceLabel;
	private JLabel aliasLabel;
	private JLabel keyPasswordLabel;
	private JLabel storeTypeLabel;
	private JLabel destLabel;
	private JLabel storePasswordLabel;
	private JTextField sourceTextField;
	private JTextField aliasTextField;
	private JTextField destTextField;
	private JPasswordField keyPasswordField;
	private JPasswordField storePasswordField;

	public ImportCertificateTaskPane() {
		initComponents();
	}

	@Override
	public void run() {
		ImportCertificateTask task = new ImportCertificateTask();
		task.setAlias(this.aliasTextField.getText());
		task.setFile(this.sourceTextField.getText());
		task.setKeyPassword(new String(this.keyPasswordField.getPassword()));
		task.setStoreType(this.storeTypeComboBox.getSelectedItem().toString());
		task.setKeyStore(this.destTextField.getText());
		task.setStorePassword(new String(this.storePasswordField.getPassword()));
		task.setTrustcacerts(this.trustCacertsCheckBox.isSelected());

		this.runTask(task);
	}

	private void initComponents() {
		setLayout(new GridBagLayout());
		GridBagConstraints gc;

		sourceLabel = new JLabel("Source");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 0;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(sourceLabel, gc);

		sourceTextField = new JTextField();
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 0;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(sourceTextField, gc);

		sourceButton = new JButton("Open...");
		gc = new GridBagConstraints();
		gc.gridy = 0;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 0);
		add(sourceButton, gc);

		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 1;
		gc.gridwidth = 3;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 0);
		add(new JSeparator(), gc);

		destLabel = new JLabel("Destination");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 2;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(destLabel, gc);

		destTextField = new JTextField();
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 2;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(destTextField, gc);

		destButton = new JButton("Open...");
		gc = new GridBagConstraints();
		gc.gridy = 2;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 0);
		add(destButton, gc);

		aliasLabel = new JLabel("Alias");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 3;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(aliasLabel, gc);

		aliasTextField = new JTextField();
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 3;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(aliasTextField, gc);

		keyPasswordLabel = new JLabel("Key Password");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 4;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(keyPasswordLabel, gc);

		keyPasswordField = new JPasswordField();
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 4;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(keyPasswordField, gc);

		storePasswordLabel = new JLabel("Store Password");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 5;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 5, 5);
		add(storePasswordLabel, gc);

		storePasswordField = new JPasswordField();
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 5;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 5, 5);
		add(storePasswordField, gc);

		storeTypeLabel = new JLabel("Store Type");
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 6;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.insets = new Insets(0, 0, 0, 5);
		add(storeTypeLabel, gc);

		storeTypeComboBox = new JComboBox(KeyTools.getSupportKeyStoreTypes());
		gc = new GridBagConstraints();
		gc.gridx = 1;
		gc.gridy = 6;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		gc.weightx = 10.0;
		gc.insets = new Insets(0, 0, 0, 5);
		add(storeTypeComboBox, gc);

		trustCacertsCheckBox = new JCheckBox("Trust Cacerts");
		gc = new GridBagConstraints();
		gc.gridy = 6;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.anchor = GridBagConstraints.BASELINE_LEADING;
		add(trustCacertsCheckBox, gc);

		// glue
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 7;
		gc.gridwidth = 3;
		gc.fill = GridBagConstraints.BOTH;
		gc.weightx = 10.0;
		gc.weighty = 10.0;
		add(Box.createGlue(), gc);

	}
}